package jeecg.mongodb.system;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class DBFileSystem {
	/**
	 * 删除文件夹下所有文件
	 * */
	public static boolean delAllFiles(File file){
		boolean flag=false;
		if(file.exists()){
			return flag;
		}
		File[] tempList = file.listFiles();
		for (File f2 : tempList) {
			if ( f2.isFile()) {
				//删除文件
				file.delete();
			}else{
				//删除文件夹里面的文件
				delAllFiles(f2);
			}
			//删除文件夹
			file.delete();
		}
		return true;
	}
	
	public static String getDocumentPath(DataBase db,String document){
		return db.getPath()+"/"+db.getDbName()+"/"+document+".txt";
	}
	
	/**
	 * 在拷贝文件时，如果某行条件满足要求则删除某一行
	 * */
	public static  void copyFileDelete(File f1,File f2,DynamicBean dynamicBean) throws IOException{
		FileReader reader = new FileReader(f1);
		FileWriter writer = new FileWriter(f2, true);
		BufferedReader br = new BufferedReader(reader);
		String str = null;
		while(( str = br.readLine()) != null) {
			DynamicBean dbean=compareObject(dynamicBean,str,true);
			if("{}".equals(str)){
				writer.write(str);  
			}else if(dbean==null){
				writer.write("\r\n"+str);  
			}
		}
		br.close();
		writer.close();
	}
	
	/**
	 * 在拷贝文件时，如果某行条件满足要求则更新某一行
	 * */
	public static  void copyFileUpdate(File f1,File f2,DynamicBean dynamicBean,String update) throws IOException{
		FileReader reader = new FileReader(f1);
		FileWriter writer = new FileWriter(f2, true);
		BufferedReader br = new BufferedReader(reader);
		String str = null;
		while(( str = br.readLine()) != null) {
			DynamicBean dbean=compareObject(dynamicBean,str,true);
			if("{}".equals(str)){
				writer.write(str);  
			}else if(dbean!=null){
				writer.write("\r\n"+update);  
			}else{
				writer.write("\r\n"+str);  
			}
		}
		br.close();
		writer.close();
	}
	
	/**
	 * 拷贝文件
	 * */
	public static void copyFile(File f1,File f2,boolean bool) throws IOException{
		FileReader reader = new FileReader(f1);
		FileWriter writer = new FileWriter(f2, bool);
		BufferedReader br = new BufferedReader(reader);
		String str = null;
		while(( str = br.readLine()) != null) {
			if("{}".equals(str)){
				writer.write(str);  
			}else{
				writer.write("\r\n"+str);  
			}
		}
		br.close();
		writer.close();
	}
	
	/**
	 * 比较obj是否满足要求，bool false：表示or查询，true：表示and查询
	 * 
	 * 返回被对比对象的Bean
	 * */
	public static DynamicBean compareObject(DynamicBean dynamicBean,String json,boolean bool){
		HashMap propertyMap=new HashMap();
		JsonParser jp=new JsonParser();
		JsonObject jo=jp.parse(json).getAsJsonObject();
		Set<Map.Entry<String,JsonElement>> set=jo.entrySet();
		for (Entry<String, JsonElement> entry : set) {
			if(bool){
				if(dynamicBean.getValue(entry.getKey())!=null){
					if(!dynamicBean.getValue(entry.getKey()).equals(entry.getValue().toString())){
						return null;
					}
				}
			}else{
				if(dynamicBean.getValue(entry.getKey()).equals(entry.getValue())){
					return BeanFactory.createQueryBean(json);
				}
			}
		} 
		if(bool){
			return BeanFactory.createQueryBean(json);
		}else{
			return null;
		}
	}
}
